#pragma once
#include <vector>
#include <string>
#include <queue>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include "RingBuffer.h"
#include "Downloader.h"
#include "Player.h"
#include "EventCallback.h"

class PlayerController {
public:
    PlayerController(size_t buffer_size = 2 * 1024 * 1024)
        : buffer_(buffer_size), 
          downloader_(buffer_, urls_),
          player_(buffer_),
          current_index_(-1),
          state_(STOPPED),
          stop_requested_(false) {}
    
    ~PlayerController() {
        Stop();
    }
    
    // 播放列表管理
    void AddToPlaylist(const std::string& url) {
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        urls_.push_back(url);
    }
    
    void ClearPlaylist() {
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        urls_.clear();
        current_index_ = -1;
    }
    
    void SetPlaylist(const std::vector<std::string>& urls) {
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        urls_ = urls;
        current_index_ = -1;
    }
    
    // 事件回调设置
    void SetEventCallback(EventCallback* callback) {
        callback_ = callback;
    }
    
    // 播放控制
    void Play() {
        if (state_ == PLAYING) return;
        
        if (state_ == STOPPED) {
            if (!player_.Initialize()) {
                if (callback_) callback_->OnPlayError("ALSA initialization failed");
                return;
            }
            
            downloader_.Start();
            player_.Start();
            state_ = PLAYING;
            
            // 开始播放第一首
            PlayNext();
        } else if (state_ == PAUSED) {
            player_.Resume();
            state_ = PLAYING;
            if (callback_) callback_->OnPlayResume();
        }
    }
    
    void Pause() {
        if (state_ != PLAYING) return;
        
        player_.Pause();
        state_ = PAUSED;
        if (callback_) callback_->OnPlayPause();
    }
    
    void Stop() {
        if (state_ == STOPPED) return;
        
        stop_requested_ = true;
        downloader_.Stop();
        player_.Stop();
        state_ = STOPPED;
        current_index_ = -1;
        if (callback_) callback_->OnPlayStop();
    }
    
    void Next() {
        if (state_ != PLAYING) return;
        PlayNext();
    }
    
    void Previous() {
        if (state_ != PLAYING || current_index_ <= 0) return;
        
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        current_index_ = std::max(0, current_index_ - 1);
        const std::string& url = urls_[current_index_];
        
        // 在实际应用中，这里需要实现歌曲切换逻辑
        if (callback_) callback_->OnPlayStart(url);
    }
    
    // 获取状态
    enum PlayerState { STOPPED, PLAYING, PAUSED };
    PlayerState GetState() const { return state_; }
    
    // 当前播放信息
    std::string GetCurrentUrl() const {
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        if (current_index_ >= 0 && current_index_ < urls_.size()) {
            return urls_[current_index_];
        }
        return "";
    }
    
    int GetCurrentIndex() const { return current_index_; }

private:
    void PlayNext() {
        std::lock_guard<std::mutex> lock(playlist_mutex_);
        
        if (urls_.empty()) return;
        
        current_index_ = (current_index_ + 1) % urls_.size();
        const std::string& url = urls_[current_index_];
        
        // 通知新歌曲开始播放
        if (callback_) callback_->OnPlayStart(url);
        
        // 在实际应用中，这里需要实现歌曲切换逻辑
        // 例如：重置缓冲区、开始下载新歌曲等
    }

    // 播放列表
    std::vector<std::string> urls_;
    mutable std::mutex playlist_mutex_;
    int current_index_;
    
    // 核心组件
    RingBuffer buffer_;
    Downloader downloader_;
    Player player_;
    
    // 状态管理
    PlayerState state_;
    std::atomic<bool> stop_requested_;
    
    // 回调接口
    EventCallback* callback_ = nullptr;
};